<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>路由独享守卫</title>
</head>

<body>
    <div id="app">
        <router-link to="/" tag="button">首页</router-link>
        <router-link to="/news">新闻</router-link>
        <router-link to="/login">登录</router-link>
        <router-view></router-view>
    </div>
    <template id="tem1">
        <h2>这是首页</h2>
    </template>
    <template id="tem2">
        <div>这是新闻页面</div>
    </template>
    <template id="tem3">
        <h2>登录页面</h2>
    </template>
</body>

</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script>
    const Home = {
        template: "#tem1"
    }
    const News = {
        template: "#tem2"
    }
    const Login = {
        template: "#tem3"
    }
    const routes = [{
        path: "/",
        component: Home
    }, {
        path: "/news",
        component: News
    }, {
        path: "/login",
        component: Login,
        /* 路由独享守卫 */
        beforeEnter: (to, from, next) => {
            console.log("登录页面");
            next();
        }
    }]
    const router = new VueRouter({
        routes
    })
    new Vue({
        el: "#app",
        router: router,
    })
</script>